<?php
if (!defined('WY_ROOT')) exit;


class weixin extends Wy_Controller
{
    
    function details() {
        $resInfoUser =$this->model('wechart')->fetch_one('weixin_config','id=1',"*");
       
        if(empty($resInfoUser)) {
            exit('error:no uid info');
        }
        if(empty($resInfoUser['wx_token'])) {
            exit('no token');
        }
        $options = array(
            'token'=> $resInfoUser['wx_token']
        );
		require_once WY_ROOT .'/includes/libs/Weixin/wechat.php';
		
        $weObj = new Wechat($options);
        $weObj->valid();
        //api接口统计
        //$M_WxStatus =Db::name('WxStatus');
//        $_where = array();
//        $M_WxStatus->setStatus($id, 'today_api');
        $type = $weObj->getRev()->getRevType();
        //$type = Wechat::MSGTYPE_TEXT;
        //默认关注时的回复内容
		$resInfoAttention =$this->model('wechart')->fetch_one('user_attention','keywords=""',"*");
        //if(empty($resInfoUser)) {
//            $weObj->text('此用户还未开通权限a')->reply();
//            exit;
//        }
//        if($resInfoUser['time_valid'] < time()) {
//            $weObj->text('此用户vip已经过期')->reply();
//            exit;
//        }
        //接收内容
        $_dataMessage = array();
		
        switch($type) {
			
            case \Wechat::MSGTYPE_TEXT:
                //关键语回复 
                $messageAll = $weObj->getRevContent();
                $_tmpData['fromUsername'] = $weObj->getRevFrom();
                $_tmpData['locationinfo'] = $weObj->getRevGeo();
                $resReturn = $this->model('wechart')->returnKeyword($messageAll, $_tmpData);
                file_put_contents("messageAll.txt",$resReturn);
                //$resReturn = $M_UserAttention->returnKeyword('抽奖', $id);
                if($resReturn['type'] == 'text') {
                    $weObj->text($resReturn['text'])->reply();
                } else if($resReturn['type'] == 'news') {
                    $weObj->news($resReturn['news'])->reply();
                }
                $_dataMessage['keywords'] = $messageAll;
                break;
            case \Wechat::MSGTYPE_EVENT:
                $eventArr = $weObj->getRevEvent();
                if($eventArr['event'] == 'subscribe') {
                    //新增关注
                    if($resInfoAttention['replay_type'] == '0') {
                        $weObj->text($resInfoAttention['contents_txt'])->reply();
                    } //else if($resInfoAttention['replay_type'] == '1') {
//						$M_WxMaterial =new MaterialModel();
//						$_return['news'] = $M_WxMaterial->getImgList($resInfoAttention['contents_img']);
//						$weObj->news($_return['news'])->reply();
//                  }
					    //file_put_contents("bbb12.txt",$weObj->getRevFrom());
//					//查询用户是否已经注册
//						$udata['wxid']=$uwxid=$weObj->getRevFrom();
//						$uwx_data=Db::name("user")->where(array("wxid"=>$uwxid))->find();
//						
                } else if($eventArr['event'] == 'unsubscribe') {
                    //取消关注
					//$M_WxCardUser = D('WxCardUser');
//					$_where_str['wxid'] = $weObj->getRevFrom();
//					$_data_str['isgz'] = 0;
//					$M_WxCardUser->saveData($_data_str,$_where_str);
//					$this->qx_gz($_where_str['wxid']);
                    //$qxdata['follow']=0;
					//Db::name("user")->where(array("wxid"=>$weObj->getRevFrom()))->update($qxdata);
                   // $M_WxStatus->setuserStatus($id, $weObj->getRevFrom());
                } else if($eventArr['event'] == 'CLICK') {
                    //菜单点击
                    $_res = $this->model('wechart')->fetch_one('weixin_menu','id='.$eventArr['key'],"*");
                    if($_res['replay_type'] == 0) {
						if($_res['contents_txt']=="账户余额"){
							$_res['contents_txt']=$this->model('wechart')->get_user_balance($weObj->getRevFrom());
						}
						if($_res['contents_txt']=="今日销售"){
							$_res['contents_txt']=$this->model('wechart')->get_user_todayorder($weObj->getRevFrom());
						}
                        $weObj->text($_res['contents_txt'])->reply();
                    } else if($_res['replay_type'] == 1) {
                        $_return['news'] =$this->model('wechart')->getImgList($_res['contents_img']);
						file_put_contents("return_news.json",json_encode($_return['news']));
                        $weObj->news($_return['news'])->reply();
                    }
                } else if($eventArr['event'] == 'VIEW') {
                    //菜单点击
                    $bind_flag=$this->model('wechart')->is_user_bind($weObj->getRevFrom());
					if($bind_flag){
						$eventArr['event'] = 'CLICK';
						$weObj->text("您还没有绑定您的账号，请在菜单中点击账号绑定，完成账号绑定再继续操作。")->reply();
					}
                }
                break;
            case \Wechat::MSGTYPE_IMAGE:
                $messageAll = $weObj->getRevPic();
                $_dataMessage['keywords']= $messageAll;
                break;
            case \Wechat::MSGTYPE_LOCATION:
                $messageAll = $weObj->getRevGeo();
				$_tmpData['fromUsername'] = $weObj->getRevFrom();
                $resReturn = $M_UserAttention->returnLocation($messageAll, $id, $_tmpData);
                if($resReturn['type'] == 'text') {
                    $weObj->text($resReturn['text'])->reply();
                } else if($resReturn['type'] == 'news') {
                    $weObj->news($resReturn['news'])->reply();
                }
                $_dataMessage['keywords']= $messageAll['x'] . ',' . $messageAll['y'];
                break;
            case \Wechat::MSGTYPE_LINK:
               $messageAll = $weObj->getRevLink();
               $_dataMessage['keywords']= $messageAll['url'];
                break;
            default:
                $_dataMessage['k_type'] = '';
                break;
        }//end switch
        //如果没返回消息的情况下
        if(count($resReturn) < 1 && count($_return) < 1) {
             //订单查询
			 $messageAll = $weObj->getRevContent();
			 $order_info=$this->model('wechart')->getorder($messageAll,$weObj->getRevFrom());
			 $weObj->text($order_info)->reply();
        }
    }
    
} ?>